package com.liang.mall.validat;

import com.google.common.collect.Sets;
import org.springframework.util.CollectionUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.Set;

public class ListValConstraintValidator implements ConstraintValidator<ListValAnnotation, Integer> {
    private Set<Integer> set = Sets.newHashSet();


    @Override
    public void initialize(ListValAnnotation constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        if (CollectionUtils.isEmpty(Arrays.asList(vals))) {
            for (int val : vals) {
                set.add(val);
            }
        }
    }

    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        if (set.contains(integer)) {
            return true;
        }
        return false;
    }
}
